var xhrutils = require('xhrutils');
const common = require('common');
cc.Class({
    extends: cc.Component,

    properties: {

    },

    // use this for initialization
    onLoad: function() {
        //长按出现删除按钮
        let self = this;
        this.node.on(cc.Node.EventType.TOUCH_START, function(event) {
            self.status = 0;
            if (self.node.getChildByName('delbtn').active == true) {
                //如果正在显示删除，则不进行定时计算
                //self.node.getChildByName('delbtn').active = false;
                self.status = 1;
            } else {
                self.scheduleOnce(function() {
                    //长按后，所有结点都显示删除
                    cc.log('show delbtn');
                    let allbooks = self.node.parent.getComponentsInChildren('bookface');
                    cc.log('allbooks ' + allbooks.length);
                    for (var i = allbooks.length - 1; i >= 0; i--) {
                        allbooks[i].node.getChildByName('delbtn').active = true;
                        allbooks[i].getComponent(cc.Animation).play();
                    }
                    cc.log('show toolbar'+cc.find('Canvas/toolbar'));
                   
                    let toolbar = cc.find('Canvas/toolbar');
                    toolbar.x = 0;
                    toolbar.y = 500;
                    toolbar.active = true;
                   
                }, 1.5);
            }


        });
        this.node.on(cc.Node.EventType.TOUCH_END, function(event) {
            
            if (this.getChildByName('delbtn').active) {
                if (self.status == 1) {
                    cc.log(' del some book. event.target' + event.target.name);
                    //这里是删除
                    self.node.destroy();
                    //从bookdata中删除，包括common中的和bookdata.json中的
                    for (var i = 0; i < common.init.bookdata.length; i++) {
                        if (common.init.bookdata[i].id == self.node.bookid) {
                            common.init.bookdata.splice(i, 1);
                            break;
                        }
                    }
                    jsb.fileUtils.writeStringToFile(JSON.stringify(common.init.bookdata),
                        jsb.fileUtils.getWritablePath() + "bookdata.json");
                    //要反向刷新书城？如果这样不如在点击书城时再进行init?
                }
            } else {
                if (self.status == 0) {
                    //进入阅读场景

                    common.init.current_bookid = this.bookid;
                    common.init.current_page = this.page;
                   
                    cc.log(common.init.current_bookid + '-------------------' + common.init.name);

                    cc.director.loadScene("book");
                } else {
                    self.unscheduleAllCallbacks();
                }
            }

        });

        //从这里自定义触发事件
        // var self=this;
        // this.node.on(cc.Node.EventType.TOUCH_END,function(e){
        //     self.common.
        // });
    },
    setBookName: function(bookname) {
        let self = this;
        this.node.getChildByName('bookname').getComponent(cc.Label).string = bookname;
        cc.log('this.node.getChildByName(\'delbtn\').active' + this.node.getChildByName('delbtn').active);
        //初始都是没有的
        this.node.getChildByName('delbtn').active = false;
        // this.node.getChildByName('delbtn').on(cc.Node.EventType.TOUCH_END, function(event) {
        //     cc.log('del ' + self.node.bookid);
        // });
    },
    setBookFace: function(url) {
            let self = this;
            xhrutils.loadNativeImg(url, function(textrue, i) {
                //cc.log('this is ' + textrue);
                // let spriteFrame=self.getComponent(cc.Sprite).spriteFrame;
                // if (self.getComponent(cc.Sprite).spriteFrame.textureLoaded()) {
                //     cc.log('sprite loaded');
                self.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(textrue);
                // }else{
                //      self.getComponent(cc.Sprite).spriteFrame.once('load', function(){
                //         cc.log('sprite not loaded');
                //         self.getComponent(cc.Sprite).spriteFrame= new cc.SpriteFrame(textrue);
                //     }, this);
                // }
            }, 0);
            //this.getComponent(cc.Sprite).spriteFrame.setTexture
        }
        
});